let _Vue
class Store {
  constructor (options) {
    this.state = _Vue.observable(options.state)
    this.mutations = options.mutations
    this.actions = options.actions
    this.handleGetters(options.getters)
  }

  handleGetters (getters = {}) {
    this.getters = {}
    for (const key in getters) {
      Object.defineProperty(this.getters, key, {
        get: () => {
          return getters[key](this.state)
        }
      })
    }
  }

  commit (mutation,  payload) {
    this.mutations[mutation](this.state, payload)
  }

  dispatch (action, payload) {
    this.actions[action](this, payload)
  }
}

function install (Vue) {
  _Vue = Vue
  _Vue.mixin({
    beforeCreate() {
      if (this.$options.store) {
        _Vue.prototype.$store = this.$options.store
      }
    }
  })
}

export default {
  Store,
  install
}
